<template>
  <div class="main_box">
    <el-row>
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <el-col :span="12">
        <div class="opration_wrap" v-has="permissionAdd">
          <el-button type="success" icon="fa fa-plus" @click="handleClickAdd()">新增</el-button>
          <el-button type="success" icon="el-icon-refresh-right" @click="reload">刷新</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <div class="search_wrap">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item prop="dictId">
            <el-input v-model="searchForm.dictId" :clearable="true" placeholder="字典类型id"></el-input>
          </el-form-item>
          <el-form-item prop="adId">
            <el-input v-model="searchForm.typeName" :clearable="true" placeholder="字典类型名"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="tableList" :fit="true">
        <el-table-column prop="dictId" label="字典ID" align="center" width="200"></el-table-column>
        <el-table-column prop="dictValue" label="字典值" align="center" width="200"></el-table-column>
        <el-table-column prop="dictLabel" label="字典信息名称" align="center" width="200"></el-table-column>
        <el-table-column prop="sort" label="排序" align="center" width="200"></el-table-column>
        <el-table-column prop="remark" label="备注" align="center" width="200"></el-table-column>
        <el-table-column prop="typeId" label="字典类型ID" align="center" width="200"></el-table-column>
        <el-table-column prop="typeName" label="字典类型名称" align="center" width="200"></el-table-column>
        <el-table-column prop="createUserName" label="创建人" align="center" width="200"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center" width="200"></el-table-column>

        <el-table-column label="操作" width="136">
          <template slot-scope="scope">
            <el-row>
              <el-button v-has="permissionEdit" @click="handleClickEditor(scope.row)" size="mini">编辑</el-button>
              <el-button v-has="permissionDelete" @click="handleClickDelete(scope.row)" size="mini">删除</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import Crumbs from '@/components/Crumbs';
import * as validators from '@/utils/validators';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import Region from '@/components/Region';
export default {
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      searchForm: {
        dictId: '',
        typeName: '',
      },
      tableList: [],

      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
        currentPage: 1,
      },
      permissionAdd: 'SYSTEM_MANAGER_SYS_DICT_LIST_ADD',
      permissionEdit: 'SYSTEM_MANAGER_SYS_DICT_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_SYS_DICT_LIST_DELETE',
    };
  },
  components: {
    Crumbs,
    Region,
  },
  mounted() {
    this.init();
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      if (from.name == '新增字典' || from.name == '编辑字典') {
        vm.init();
      }
    });
  },
  methods: {
    init() {
      this.listTable();
      this.getTableHeight();
    },

    handleClickAdd() {
      this.$router.push({ path: '/sys/dictAdd' });
    },

    handleClickEditor(row) {
      this.$router.push({ path: '/sys/dictEdit', query: { id: row.dictId } });
    },

    handleClickDelete(row) {
      let txt = '删除';
      this.$confirm('确定' + txt + '该字典信息吗？', message.TITLE_PROMPT, {}).then(() => {
        let deleteData = {
          dictId: row.dictId,
        };
        this.$Delete(this.$Config.api.dictDelete, deleteData)
          .then((res) => {
            this.listTable();
            this.$message({
              type: 'success',
              message: message.MSG_ACTION_SUCCESS,
            });
          })
          .catch((error) => {
            if (error.msg) {
              this.$message.error(message.MSG_ACTION_FAIL);
            }
          });
      });
    },

    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },

    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    listTable() {
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        dictId: this.searchForm.dictId,
        typeName: this.searchForm.typeName,
      };
      this.fetchData(data);
    },
    fetchData(data) {
      this.$emit('show-loading', true);
      this.$Get(this.$Config.api.dictList, data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.page.currentPage = res.data.pageNum;
          this.$emit('show-loading', false);
        })
        .catch((error) => {
          console.error(error);
          this.$emit('show-loading', false);
        });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
